<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="dialogForm" :model="form" :rules="rules" label-width="100px" label-position="left">
					<el-form-item label="排序号">
						<el-input-number v-model="form.sortNo" placeholder="排序号"></el-input-number>
					</el-form-item>
					<el-form-item label="姓名" prop="userName">
						<el-input v-model="form.userName" placeholder="姓名"></el-input>
					</el-form-item>
					<el-form-item label="性别" prop="sex">
						<el-radio-group v-model="form.sex">
							<el-radio-button label="1">男</el-radio-button>
							<el-radio-button label="0">女</el-radio-button>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="类型" prop="bookType">
						<sc-select v-model="form.bookType" :params="{code:'linkManType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
					<el-form-item label="职务名称" prop="levelName">
						<el-input v-model="form.levelName" placeholder="职务名称"></el-input>
					</el-form-item>
					<el-form-item label="部门名称" prop="deptName">
						<el-input v-model="form.deptName" placeholder="部门名称"></el-input>
					</el-form-item>
					<el-form-item label="手机号" prop="mobileNo">
						<el-input v-model="form.mobileNo" placeholder="手机号"></el-input>
					</el-form-item>
					<el-form-item label="电话" prop="tel">
						<el-input v-model="form.tel" placeholder="电话"></el-input>
					</el-form-item>
					<el-form-item label="邮箱" prop="email">
						<el-input v-model="form.email" placeholder="邮箱"></el-input>
					</el-form-item>
					<el-form-item label="地址" prop="address">
						<el-input v-model="form.address" placeholder="地址"></el-input>
					</el-form-item>
					<el-form-item label="是否共享" prop="isShare">
						<el-radio-group v-model="form.isShare">
							<el-radio-button label="1">共享</el-radio-button>
							<el-radio-button label="0">不共享</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-form>
			</el-main>
		</el-container>
		<el-footer style="text-align: center">
			<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
			<el-button @click="visible=false">取消</el-button>
		</el-footer>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: '创建联系人',
				edit: '编辑联系人'
			},
			form: {
				recordId: "",
				sortNo: 0,
				userName: '',
				sex: '1',
				bookType: '1',
				levelName: '',
				deptName: '',
				mobileNo: '',
				tel: '',
				email: '',
				address: '',
				isShare: '1'
			},
			visible: false,
			isSaveing: false,
			rules: {
				levelName: [
					{required: true, message: '职务名称不能为空', trigger: 'blur'}
				],
				userName: [
					{required: true, message: '姓名不能为空', trigger: 'blur'}
				],
				mobileNo: [
					{required: true, message: '手机号不能为空', trigger: 'blur'}
				]
			},
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.info.addressBook.insertAddressBook.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.info.addressBook.updateAddressBook.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.recordId = data.recordId;
			this.form.sortNo = data.sortNo;
			this.form.userName = data.userName;
			this.form.sex = data.sex;
			this.form.bookType = data.bookType;
			this.form.levelName = data.levelName;
			this.form.deptName = data.deptName;
			this.form.mobileNo = data.mobileNo;
			this.form.tel = data.tel;
			this.form.email = data.email;
			this.form.address = data.address;
			this.form.isShare = data.isShare;
		}
	}
}
</script>

<style>
</style>
